<?php
/**
 * Edition d'un document
 */
require_once("include/setup.php");
require_once(CLASS_PATH.'document.class.php');
require_once(HELPER_PATH.'file.helper.php');
LoginHelper::controlAccess(array('type_droits_user' => '>=1'));


$PATH_DOCUMENTS = RACINE_PATH.'files/documents/';
$document = new Document($_REQUEST['id']);

if (!empty($document->id_document)) {
	LoginHelper::controlAccess(array('id_user' => $document->id_user));
}

$error = false;
if (!empty($_POST['document'])) {
	$edited = true;
	$havefile = false;
	$tempfilepath = "";
	$isDropboxfile = false;
	if (!empty($_FILES['document'])) {
		$file = $_FILES['document'];
		
		switch($file['error']) {
			case UPLOAD_ERR_OK: // Succès
				$havefile = true;
				$tempfilepath = $file['tmp_name'];
				$document->filename = $file['name'];
				break;
				
			case UPLOAD_ERR_FORM_SIZE:
				$size = $_POST['MAX_FILE_SIZE'];
				Notification::add(Notification::$ERROR, "La taille du fichier ne doit pas excéder ".file_sizeHumanReadable($size));
				$error = true;
				break;
			case UPLOAD_ERR_INI_SIZE:
				$size = ini_get('upload_max_filesize');
				Notification::add(Notification::$ERROR, "La taille du fichier ne doit pas excéder ".file_sizeHumanReadable($size));
				$error = true;
				break;
				
			case UPLOAD_ERR_NO_FILE:
				break;
				
			default:
				Notification::add(Notification::$ERROR, "Impossible de prendre en charge le fichier. Merci de ré-essayer.");
				$error = true;
				break;
		}
	}
	
	$data = $_POST['document'];
	if (!empty($data['id_document'])) {
		$document->load($data['id_document']);
	}
	
	if (!$havefile && isset($data['dropboxfile'])) {
		$havefile = true;
		$isDropboxfile = true;
		$tempfilepath = $data['dropboxfile'];
		$filename = substr($data['dropboxfile'], strrpos($data['dropboxfile'], '/')+1);
		$document->filename = rawurldecode($filename);
	}
	
	$document->init($data);
	$mode_add = empty($document->id_document);
	
	
	$document->id_user = LoginHelper::getIdUserConnected();
	
	if (empty($document->titre)) {
		Notification::add(Notification::$ERROR, "Merci de renseigner un titre.");
		$error = true;
	}
	if (!$havefile && $mode_add) {
		Notification::add(Notification::$ERROR, "Merci de renseigner le fichier.");
		$error = true;
	}

	if (!$error) {
		if ($document->save()) {
			if ($havefile) {
				$ext = file_get_extension($document->filename);
				$filepath = $document->id_document.$ext;
				$completefilepath = $PATH_DOCUMENTS.$filepath;
				if (!$isDropboxfile) {
					move_uploaded_file($tempfilepath, $completefilepath);
				}
				else {
					$contents = file_get_contents($tempfilepath);
					file_put_contents($completefilepath, $contents);
				}
				$document->filepath = $filepath;
				$document->save();
				Notification::add(Notification::$SUCCESS, "Le document a été enregistré"); 
			}
		}
		else {
			Notification::add(Notification::$ERROR, "Impossible d'enregistrer le document.");
		}
	}
	
}

$v = new JapwordView();
$v->assign('document', $document);
$v->assign('breadcrumb',array(
	"Mes documents" => 'document_mylist.php',
	"Document" => ''
));
$v->addNotifications();

if (!empty($document->id_document)) {
	$v->display('document_edit.html');
}
else {
	$v->display('document_add.html');
}
?>